package esb.chapter9.restaurant.servicemix;

import javax.jbi.messaging.NormalizedMessage;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.xml.transform.Source;

import org.apache.servicemix.jbi.jaxp.StringSource;
import org.apache.servicemix.jms.endpoints.DefaultConsumerMarshaler;
import org.apache.servicemix.jms.endpoints.SimpleDestinationChooser;

public class ReplyToConsumerMarshaler extends DefaultConsumerMarshaler {

	protected void populateMessage(Message message, NormalizedMessage normalizedMessage) throws Exception {
        if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            normalizedMessage.setProperty(SimpleDestinationChooser.DESTINATION_KEY, 
            		message.getJMSReplyTo());
            Source source = new StringSource(textMessage.getText());
            normalizedMessage.setContent(source);
        } else {
            throw new UnsupportedOperationException("JMS message is not a TextMessage");
        }
	}

}
